package com.ex.touristtravels;

import javax.security.auth.PrivateCredentialPermission;

import com.ex.touristtravels.database.DatabaseHelper;
import com.ex.touristtravels.util.MyDialog;
import com.ex.touristtravels.util.MyDialog.onClickDialogListener;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RegisterActivity extends Activity implements OnClickListener {

	private Button bt_ok; // 完成注册的按钮
	private EditText et_username; // 用户名编辑框
	private EditText et_password1; // 密码编辑框1
	private EditText et_password2; // 确认密码编辑框
	private MyDialog dialogg;

	private String usernameStr = null;
	private String passwordStr1 = null;
	private String passwordStr2 = null;

	private DatabaseHelper dbHelper; // 数据库操作对象
	private static final String DB_NAME = "UserInfo.db"; // 数据库名称
	private static final int VERSION = 1; // 数据库版本

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initView(); 
	}

	// 初始化控件
	private void initView() {
		dbHelper = new DatabaseHelper(this, DB_NAME, null, VERSION); // 实例化数据库操作对象
		bt_ok = (Button) findViewById(R.id.btnOK);
		et_username = (EditText) findViewById(R.id.etUser);
		et_password1 = (EditText) findViewById(R.id.etPwd);
		et_password2 = (EditText) findViewById(R.id.etPwd2);
		bt_ok.setOnClickListener(this);
	}

	// 完成注册的监听事件
	@Override
	public void onClick(View v) {
		usernameStr = et_username.getText().toString().trim();
		passwordStr1 = et_password1.getText().toString().trim();
		passwordStr2 = et_password2.getText().toString().trim();
		if (usernameStr.equals("") || passwordStr1.equals("")
				|| passwordStr2.equals("")) {
			on_click1();   //注册有误，重新填写用户信息

		} 
		else if (passwordStr1.equals(passwordStr2)) { // 若两次输入的密码符合用户信息，则将用户信息插入到User表中
			if (register(usernameStr, passwordStr1)) {
				Toast.makeText(RegisterActivity.this, "插入数据库成功",
						Toast.LENGTH_SHORT);
			}
			to_login();  //跳转到登录界面

		} else {
			on_click2();   //注册密码有误，重新填写信息
			et_password1.setText("");
			et_password2.setText("");
		}
	}

	// 顶部的返回按钮图标（返回登录界面）
	public void login_back(View v) {
		Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
		startActivity(intent);
		this.finish();
	}

	// 插入用户的数据（用户名与密码）大数据库中
	public boolean register(String newusername, String newpassword) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String sql = "insert into User(username,password) values(?,?)";
		Object object[] = { newusername, newpassword };
		db.execSQL(sql, object);
		return true;

	}

	// 重新填写用户信息
	private void on_click1() {
		dialogg = new MyDialog(RegisterActivity.this);
		dialogg.initUI("注册错误", "账号或者密码不能为空！", "重新填写");
		dialogg.setOnDialogClickListener(new onClickDialogListener() {

			@Override
			public void onclickButton() {
				dialogg.closeDialog();
			}
		});
		dialogg.showDialog();

	}

	// 重新填写密码
	private void on_click2() {
		dialogg = new MyDialog(RegisterActivity.this);
		dialogg.initUI("注册错误", "两次密码输入不一致，请重新输入", "重新输入");
		dialogg.setOnDialogClickListener(new onClickDialogListener() {

			@Override
			public void onclickButton() {
				// TODO Auto-generated method stub
				dialogg.closeDialog();
			}
		});
		dialogg.showDialog();
	}

	// 注册成功，跳转到登录界面
	private void to_login() {
		dialogg = new MyDialog(RegisterActivity.this);
		dialogg.initUI("注册成功", "点击按钮跳转到登录界面", "跳转");
		dialogg.setOnDialogClickListener(new onClickDialogListener() {

			@Override
			public void onclickButton() {
				Intent intent = new Intent(RegisterActivity.this,
						LoginActivity.class);
				startActivity(intent);
				Toast.makeText(RegisterActivity.this, "注册完成，跳转到了登录界面",
						Toast.LENGTH_SHORT).show();
				finish();
			}
		});
		dialogg.showDialog();
	}

}
